package cn.mrlong.aavdemo.audiorecord;

import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.Environment;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import cn.mrlong.aavdemo.R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class AudioRecordActivity extends AppCompatActivity {
    private AudioRecord audioRecord;//声明audioRecord对象

    private boolean isRecording = false;

    private int recordBufSize = 0;//声明recordBuffer的大小字段
    private int sampleRateInHz = 44100;
    private int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
    private int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

    private String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/temp.raw";
    private String outPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/outdatefile.wav";



    private Thread recordingThread = new Thread() {
        @Override
        public void run() {
            super.run();
            FileOutputStream os = null;
            byte audiodata[] = new byte[recordBufSize];
            int readsize = 0;
            try {
                File file = new File(filePath);
                if (file.exists()) {
                    file.delete();
                }
                os = new FileOutputStream(file);

            } catch (Exception e) {
                e.printStackTrace();
            }
            if (null != os) {
                while (isRecording) {
                    readsize = audioRecord.read(audiodata, 0, recordBufSize);
                    if (AudioRecord.ERROR_INVALID_OPERATION != readsize) {
                        // 如果读取音频数据没有出现错误，就将数据写入到文件
                        try {
                            os.write(audiodata);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_audio_record);
        findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createAudioRecord();
            }
        });
        findViewById(R.id.stop).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stop();
            }
        });
    }

    public void createAudioRecord() {
        recordBufSize = AudioRecord.getMinBufferSize(sampleRateInHz, channelConfiguration, audioFormat); //audioRecord能接受的最小的buffer大小
        audioRecord = new AudioRecord(MediaRecorder.AudioSource.DEFAULT, sampleRateInHz,
                channelConfiguration, audioFormat, recordBufSize * 2);
        audioRecord.startRecording();
        isRecording = true;
        recordingThread.start();
    }

    public void stop() {
        try {
            audioRecord.stop();
            audioRecord.release();
            audioRecord = null;
            isRecording = false;
            recordingThread.interrupt();
            recordingThread = null;
        } catch (Exception e) {
            e.printStackTrace();
        }

        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    //.raw将转化为.wav文件"
                    PcmToWavUtil pcmToWavUtil = new PcmToWavUtil(sampleRateInHz, channelConfiguration, audioFormat);
                    pcmToWavUtil.pcmToWav(filePath, outPath);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


}